package test.blued.com.casecontroller.commenthod;

import android.os.Environment;

import com.cybozu.labs.langdetect.Detector;
import com.cybozu.labs.langdetect.DetectorFactory;
import com.cybozu.labs.langdetect.LangDetectException;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

/**
 * Created by BlueCity on 2017/6/12.
 */

public class CommandMethod {

    public String SDPath = Environment.getExternalStorageDirectory() + "/";

    public void theCMD(String cmdStr) {
        try {
            Runtime.getRuntime().exec(cmdStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //复制文件到手机
    public void copyFile(InputStream sourceFile, String targetFile){
        String path = SDPath + "QABlued/";
        File pathDir = new File(path);
        File tf = new File(path + targetFile);

        if(!pathDir.exists()){
            pathDir.mkdir();
        }
        if(!tf.exists()){
            try {
                tf.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if(tf.length() != 0){
            return;
        }
        if(sourceFile == null){
            return;
        }

        BufferedReader in = null;
        BufferedWriter out = null;
        try {
            String str = null;
            in = new BufferedReader(new InputStreamReader(sourceFile));
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tf)));
            while ((str = in.readLine()) != null) {
                out.write(str);
                out.newLine();
                out.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public List<String> getFileNames() {
        String tempP = SDPath + "QABlued/";
        String path = tempP + "TestLog";
        List<String> listName = new ArrayList<String>();
        File fileDir = new File(path);
        File[] files = null;
        if (!new File(tempP).exists() || !fileDir.exists()) {
            return listName;
        } else {
            files = fileDir.listFiles();
        }

        for(File file : files){
            listName.add(file.getName());
        }
        return listName;
    }

    public String xmlToHtml(String xmlNameStr) {
        String logPath = SDPath + "QABlued/TestLog/";
        String name = xmlNameStr.substring(0, xmlNameStr.length()-4);
        // 创建XML的文件输入流
        String xmlName = logPath + xmlNameStr;
        FileInputStream fis = null;
        FileInputStream fis1 = null;
        String htmlName = logPath + name + ".html";
        PrintStream stm = null;
        try {
            fis = new FileInputStream(xmlName);
            fis1 = new FileInputStream(SDPath + "QABlued/" + "junit-noframes.xsl");
            stm = new PrintStream(new File(htmlName));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 创建XML文件的输入流
        Source source = new StreamSource(fis);

        // 创建XSL文件的输入流
        Source template = new StreamSource(fis1);

        // 讲转换后的结果输出到 stm 中
        Result result = new StreamResult(stm);
        // 根据XSL文件创建准个转换对象
        Transformer transformer = null;
        try {
            transformer = TransformerFactory.newInstance().newTransformer(template);
            // 处理xml进行交换
            transformer.transform(source, result);
            // 关闭文件流
            fis1.close();
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return htmlName;
    }

    //识别语言
    public String getLanguage(String language){
        try
        {
//            DetectorFactory.loadProfile("/data/data/test.blued.com.casecontroller/language_profiles");
            DetectorFactory.loadProfile("./language_profiles");
        } catch (LangDetectException e)
        {
            e.printStackTrace();
        }
        Detector detect;
        try
        {
            detect = DetectorFactory.create();
            detect.append(language);
            return detect.detect();
        } catch (LangDetectException e)
        {
            e.printStackTrace();
        }
        return "error!!";
    }

}
